var target : Transform;
var distance = 3;
 
var xSpeed = 250.0;
var ySpeed = 120.0;
 
var yMinLimit = -20;
var yMaxLimit = 80;
 
private var x = 0.0;
private var y = 0.0;
 
var smoothTime = 0.3;
 
private var xSmooth = 0.0;
private var ySmooth = 0.0; 
private var xVelocity = 0.0;
private var yVelocity = 0.0;
 
private var posSmooth = Vector3.zero;
private var posVelocity = Vector3.zero;

private var qIsDown = false;
private var eIsDown = false;

//Temp CLAMP values. Use map X and Z length to set clamp
var XPOSCLAMP : float = 30;
var XNEGCLAMP : float = 0;
var ZPOSCLAMP : float = 60;
var ZNEGCLAMP : float = 0;
 
 
@script AddComponentMenu("Camera-Control/Mouse Orbit smoothed")
 
function Start () {
    var angles = transform.eulerAngles;
    x = angles.y;
    y = angles.x;
    
    this.transform.parent.gameObject.transform.position.y = 4.5;
 
    // Make the rigid body not change rotation
    if (rigidbody)
        rigidbody.freezeRotation = true;
}
 
function LateUpdate () {
    if (target) {
   		if(Input.GetKeyDown("q"))
   			qIsDown = true;
   		if (Input.GetKeyUp("q"))
   			qIsDown = false;
   		if(Input.GetKeyDown("e"))
   			eIsDown = true;
   		if (Input.GetKeyUp("e"))
   			eIsDown = false;
   			
   		if (qIsDown)
			x-= xSpeed;
		if (eIsDown)
			x+= xSpeed;	

       // x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
        //y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
        xSmooth = Mathf.SmoothDamp(xSmooth, x, xVelocity, smoothTime);
        ySmooth = Mathf.SmoothDamp(ySmooth, y, yVelocity, smoothTime);
 
        ySmooth = ClampAngle(ySmooth, yMinLimit, yMaxLimit);
 
        var rotation = Quaternion.Euler(ySmooth, xSmooth, 0);
 
       // posSmooth = Vector3.SmoothDamp(posSmooth,target.position,posVelocity,smoothTime);
 
        posSmooth = target.position; // no follow smoothing
 
        transform.rotation = rotation;
        transform.position = rotation * Vector3(0.0, 0.0, -distance) + posSmooth;   
        
        setPerimeterClamp();
		//this.transform.parent.gameObject.transform.position.y = 4.5;
    }
}
 
static function ClampAngle (angle : float, min : float, max : float) {
    if (angle < -360)
        angle += 360;
    if (angle > 360)
        angle -= 360;
    return Mathf.Clamp (angle, min, max);
}

//Set the camera position to an object
function setCameraPos( xpos:float, zpos: float) {
	this.transform.parent.gameObject.transform.position.x = xpos;
	this.transform.parent.gameObject.transform.position.z = zpos;
}

function setPerimeterClamp() {
	if( this.transform.parent.gameObject.transform.position.x >= XPOSCLAMP )
		this.transform.parent.gameObject.transform.position.x = XPOSCLAMP;
	else if( this.transform.parent.gameObject.transform.position.x <= XNEGCLAMP)
		this.transform.parent.gameObject.transform.position.x = XNEGCLAMP;
		
	if( this.transform.parent.gameObject.transform.position.z >= ZPOSCLAMP )
		this.transform.parent.gameObject.transform.position.z = ZPOSCLAMP;
	else if( this.transform.parent.gameObject.transform.position.z <= ZNEGCLAMP)
		this.transform.parent.gameObject.transform.position.z = ZNEGCLAMP;		

}